﻿#include <iostream>
#include <chrono>
#ifdef _WIN32
#include <windows.h>
#define KEYDOWN(vk) ( 0x8000 & ::GetAsyncKeyState(vk) )
#endif

#include <signal.h>
#include "ffmpegthread.h"
extern "C" {
    #include <libavutil/avutil.h>
}
using namespace std;

static bool quit=false;

static void on_exit(int code){
    quit = true;
}
#ifndef _WIN32
void clear_sem_t(const std::string id){
    if(id.size()==0)
        return;
    sem_t* m_pSem=sem_open(id.data(),O_RDWR);
    if(m_pSem != SEM_FAILED){
        int v = 0;
        sem_getvalue(m_pSem,&v);
        printf("current semphore value is:%d\n",v);
        sem_close(m_pSem);
        sem_unlink(id.data());
    }
}
#endif

int _main(int argc, char** argv){
    FFmpegThread* fply = new FFmpegThread();
    bool res = fply->open_input_stream("rtsp://admin:qb@CBR0077@192.168.0.235:554/h264/ch1/main/av_stream");
    if(res&&fply->input_vid>=0)
        res = fply->init_decode_context(fply->input_vid);
    if(res&&fply->input_aid>=0)
        res = fply->init_decode_context(fply->input_aid);
    if(res){
        res = fply->init_encode_context(AVMEDIA_TYPE_VIDEO);
        res = fply->init_encode_context(AVMEDIA_TYPE_AUDIO);
        if(res){
            /*将视频流保存至本地或者推送至流媒体服务器*/
            //res = fply->open_output_stream("rtsp://192.168.0.200/www/pusher/live01","rtsp");
            res = fply->open_output_stream("E:/MyqtCodes/181.mp4");
            /*将视频流放入共享内存，以便其他进程IPC获取*/
            //fply->set_enable_ipc("20220216","20220216mutex");
            //fply->allocate_ipc_resource();
			/*将识别结果渲染进视频流*/
			//char desc[]="drawbox=box_source=side_data_detection_bboxes:color=red[detect];[detect]drawtext=box=1:boxcolor=white:fontfile=/xx/xx/stxingka.ttf:fontcolor=red:fontsize=30:text_source=side_data_detection_bboxes";
			//fply->update_filter_description(AVMEDIA_TYPE_VIDEO,desc);
            fply->start();
            while(1){
                std::this_thread::sleep_for(std::chrono::seconds(1));
                #ifdef _WIN32
                if (KEYDOWN(VK_ESCAPE))//ESC for windows, Ctrl+C for linux.
                    on_exit(0);
                #endif
                if(quit)
                    break;
            }
        }
    }
    fply->terminate();
    fply->close_input_stream();
    fply->close_output_stream();
    delete fply;
    return 0;
}

int main(int argc, char** argv){

#ifndef _WIN32
    //clear_sem_t("20211207_mutex");
    signal(SIGINT,on_exit);
#endif
    std::chrono::time_point<std::chrono::system_clock> begin = std::chrono::system_clock::now();
    _main(argc,argv);
    std::chrono::time_point<std::chrono::system_clock> end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed = end-begin;
    printf("time = %lf s\n",elapsed.count());
    return 0;
}

/*
* 硬件加速推流
    ffmpeg.exe -hwaccel cuvid -c:v h264_cuvid -i "rtsp://admin:qb@CBR007@192.168.0.181:554/h264/ch1/main/av_stream" -vcodec h264_nvenc -zerolatency 1 -preset fast -an -f rtsp rtsp://192.168.0.200/www/pusher/live01
*/
